//
//  COMarketAlertView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/7.
//

import UIKit

typealias marketCertainBlock = ((_ model:MarketModel, _ type:Int) -> Void)

class COMarketAlertView: COBaseAlertView,UITextViewDelegate{

    var certainBlock:marketCertainBlock?
    
    var upgradeModel = MarketModel()
    
    lazy var topView: UIView = {
        let top = UIView()
        top.backgroundColor = UIColor.white
        top.layer.masksToBounds = true
        top.layer.cornerRadius = PTW(20)
        return top
    }()
    
    lazy var tipLbl: UILabel = {
        let tip = UILabel()
        tip.textColor = UIColor.colorWithHex("#2C255B")
        tip.font = UIFont.pingfangFont(ofSize: PTW(20), weight: .semibold)
        tip.text = "Notice"
        return tip
    }()

    lazy var noticeLbl: UITextView = {
        let tip = UITextView()
        tip.textColor = UIColor.colorWithHex("#7863A6")
        tip.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .semibold)
        tip.backgroundColor = .clear
        tip.delegate = self
        return tip
    }()
    
    lazy var upgradeBtn: UIButton = {
        let i = UIButton()
        i.setTitle("Upgrade new App", for: .normal)
        i.setTitleColor(.white, for: .normal)
        i.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(17), weight: .semibold)
        i.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF").cgColor,UIColor.colorWithHex("#FF3DE1").cgColor], direction: .Horizontal, range: PTW(225))
        i.layer.cornerRadius = PTW(65/2)
        i.layer.masksToBounds = true
        i.addTarget(self, action: #selector(upgradeAction), for: .touchUpInside)
        return i
    }()
    
    lazy var detailLbl: UILabel = {
        let tip = UILabel()
        tip.textColor = .white
        tip.font = UIFont.pingfangFont(ofSize:PTW(15), weight: .semibold)
        tip.numberOfLines = 0
        tip.addTap { [weak self] ges in
            self?.emailAction()
        }
        return tip
    }()
    
    init(model:MarketModel){
        super.init(type: .Center)
        NotificationCenter.default.post(name: .RandomTimeRunNotification, object: nil,userInfo: ["type":1])
        self.upgradeModel = model
        self.contentView?.backgroundColor = UIColor.clear
        self.contentView?.addSubview(self.topView)
        self.topView.snp.makeConstraints { make in
            make.left.top.right.equalTo(self.contentView!)
            make.height.equalTo(PTW(420))
            make.bottom.equalTo(self.contentView!)
        }
        self.topView.addSubview(self.tipLbl)
        self.tipLbl.snp.makeConstraints { make in
            make.top.equalTo(self.topView).offset(PTW(20))
            make.centerX.equalTo(self.topView)
            make.height.equalTo(PTW(30))
        }
        self.topView.addSubview(self.upgradeBtn)
        self.upgradeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: PTW(225), height: PTW(65)))
            make.bottom.equalTo(self.topView).offset(-PTW(25))
            make.centerX.equalTo(self.topView)
        }
        self.topView.addSubview(self.detailLbl)
        self.detailLbl.snp.makeConstraints { make in
            make.left.equalTo(self.topView).offset(PTW(25))
            make.right.equalTo(self.topView).offset(-PTW(25))
            make.bottom.equalTo(self.upgradeBtn.snp.top).offset(-PTW(30))
        }
        self.topView.addSubview(self.noticeLbl)
        self.noticeLbl.snp.makeConstraints { make in
            make.left.equalTo(self.topView).offset(PTW(20))
            make.right.equalTo(self.topView).offset(-PTW(20))
            make.bottom.equalTo(self.detailLbl.snp.top).offset(-PTW(10))
            make.top.equalTo(self.tipLbl.snp.bottom).offset(PTW(20))
        }
        self.noticeLbl.text = model.illustrate
        let emailleftStr = "E-mail: "
        let emailRIghtStr = model.email
        let markAttribute = NSMutableAttributedString(string: emailleftStr+emailRIghtStr)
        let attr: [NSAttributedString.Key : Any] = [.foregroundColor: UIColor.colorWithHex("#25D495")]
        markAttribute.addAttributes(attr, range: NSRange(location: emailleftStr.count, length: emailRIghtStr.count))
        self.detailLbl.attributedText = markAttribute
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 版本更新
    @objc func upgradeAction(){
        dissMiss()
        if ((self.certainBlock) != nil){
            self.certainBlock!(self.upgradeModel,1)
        }
    }
    
    // 邮箱点击
    func emailAction(){
        dissMiss()
        if ((self.certainBlock) != nil){
            self.certainBlock!(self.upgradeModel,2)
        }
    }
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        if (textView.isFirstResponder){
            return true
        }
        return false
    }

}
